home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / online / fidonetts / 3csrc.lzh / 3testout.c < prev    next >
C/C++ Source or Header  |  1992-05-03  |  4KB  |  146 lines

  1. /*
  2.  * quick test whipped up to create SAMPLE.3KT in archive from my message
  3.  * base.  this is NOT a good example of how to access XBBS msg bases --
  4.  * I took many potentially lethal shortcuts.  freq XBBSMSG.LZH for a
  5.  * decent API if you're curious.
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <io.h>
  12. #include <fcntl.h>
  13. #include <share.h>
  14. #include <time.h>
  15. #include "xmsg.h"
  16. #include "3mail.h"
  17.  
  18. extern int stripjnk3 (char *a,int len);
  19. extern char * fido_2_type3a (char *a);  /* from 3supp.c */
  20.  
  21.  
  22.  
  23. int main (void) {
  24.  
  25.   int    handlet,handled,phndl = -1,x,error;
  26.   long   sized;
  27.   XMSG   xmsg;
  28.   char  *tbuf,*p,*pp;
  29.   PHDR3  pkt3;
  30.   MHDR3  msg3;
  31.   char   from[80],to[80],date[20],id[133];
  32.   time_t seed;
  33.  
  34.   pkt3.head = NULL;
  35.   pkt3.from = "Fidonet#1:380/20";
  36.   pkt3.to = NULL;
  37.   pkt3.creator = "3TESTOUT";
  38.   pkt3.pword = NULL;
  39.   pkt3.area = "NET_DEV";
  40.   msg3.head = NULL;
  41.   msg3.area = NULL;
  42.   msg3.ref = NULL;
  43.   msg3.from = from;
  44.   msg3.to = to;
  45.   msg3.date = date;
  46.   msg3.id = id;
  47.   seed = time(NULL);
  48.  
  49.   handled = sopen("E:/XBBS2/MSG/XDATA.01b",O_RDONLY | O_BINARY,SH_DENYNO);
  50.   if(handled != -1) {
  51.     lseek(handled,0L,SEEK_END);
  52.     sized = tell(handled);
  53.     lseek(handled,0L,SEEK_SET);
  54.     sized /= (long)sizeof(XMSG);  /* how many msgs, but we won't use it */
  55.     tbuf = (char *)malloc(32767);
  56.     if(tbuf) {
  57.       handlet = sopen("E:/XBBS2/MSG/XTEXT.01b",O_RDONLY | O_BINARY,SH_DENYNO); /* happens to be NET_DEV >;-) */
  58.       if(handlet != -1) {
  59.         for(x = 0;x < 15;x++) {
  60.           if(read(handled,&xmsg,sizeof(XMSG)) != sizeof(XMSG)) {
  61.             printf("\nMessage data read error\n");
  62.             break;
  63.           }
  64.           if(xmsg.m_attr & MSGPACKED) continue; /* ignore compressed msgs -- too much code to include */
  65.           if(lseek(handlet,xmsg.start,SEEK_SET) == -1L) {
  66.             printf("\nMessage text seek error\n");
  67.             break;
  68.           }
  69.           if(read(handlet,tbuf,xmsg.length) >= (int)xmsg.length - 1) {
  70.  
  71.             /* create packet, write header */
  72.  
  73.             phndl = wopen_3pkt(phndl,"SAMPLE.3KT",&pkt3,&error);
  74.             if(error) {
  75.               printf("\nPacket creation error #%d\n",error);
  76.               break;
  77.             }
  78.  
  79.             if(!xmsg.o_zone) xmsg.o_zone = 1;
  80.             sprintf(from,"%s@Fidonet#%u:%u/%u",xmsg.from,xmsg.o_zone,
  81.                     xmsg.orig_net,xmsg.orig);
  82.             if(xmsg.o_point) sprintf(&from[strlen(from)],".%u",xmsg.o_point);
  83.             sprintf(id," %lx",seed + (long)x);
  84.             strcpy(to,xmsg.to);
  85.             strcat(to,"@");
  86.             msg3.subj = xmsg.subj;
  87.             p = fido_2_type3a(xmsg.date);
  88.             if(p) strcpy(date,p);
  89.             else {
  90.               printf("\nBad date \"%s\" -- msg skipped.\n",xmsg.date);
  91.               continue;
  92.             }
  93.             p = tbuf;
  94.             while((p = strchr(p,'\01')) != NULL) {  /* strip kludges */
  95.               if((pp = strchr(p,'\r')) != NULL) {
  96.                 memmove(p,pp + 1,strlen(pp));
  97.               }
  98.               else p++;
  99.             }
  100.  
  101.             /* write msg to packet */
  102.  
  103.             write_3msghdr(phndl,&msg3,&error);
  104.             if(error) {
  105.               printf("\nMsg hdr write error #%d\n",error);
  106.               break;
  107.             }
  108.             stripjnk3(tbuf,strlen(tbuf));
  109.             if(write(phndl,tbuf,strlen(tbuf)) < 0) {
  110.               printf("\nMsg text write error\n");
  111.               break;
  112.             }
  113.             if(write(phndl,"\0",2) < 2) {  /* note wopen_pkt() backs up 1 */
  114.               printf("\nMsg text termination error\n");
  115.               break;
  116.             }
  117.  
  118.             printf("%d\r",x + 1);
  119.           }
  120.           else {
  121.             printf("\nMessage text read error\n");
  122.             break;
  123.           }
  124.         }
  125.         phndl = wclose_3pkt(phndl);
  126.         close(handlet);
  127.         free(tbuf);
  128.         close(handled);
  129.         printf("\nComplete\n");
  130.       }
  131.       else {
  132.         free(tbuf);
  133.         close(handled);
  134.         printf("\nCouldn't open text file\n");
  135.       }
  136.     }
  137.     else {
  138.       close(handled);
  139.       printf("\nOut of memory\n");
  140.     }
  141.   }
  142.   else printf("\nCouldn't open data file\n");
  143.  
  144.   return 0;
  145. }
  146.